function formatTime(time) {
  var minute = Math.floor(time / 60) % 60;
  var second = Math.floor(time) % 60;
  return (minute < 10 ? '0' + minute : minute) + ':' + (second < 10 ? '0' + second : second)
}
Page({
  data: {
    modeImg: "/images/loop.png",
    item: 0,
    playlist: [
      {
        id: 1,
        title: '祝你生日快乐',
        singer: '小丽',
        src: 'http://127.0.0.1:3000/1.mp3',
        coverImgUrl: '/images/cover.jpg'
      },
      {
        id: 2,
        title: '劳动最光荣',
        singer: '小朋',
        src: 'http://127.0.0.1:3000/2.mp3',
        coverImgUrl: '/images/cover.jpg'
      },
      {
        id: 3,
        title: '龙的传人',
        singer: '小华',
        src: 'http://127.0.0.1:3000/3.mp3',
        coverImgUrl: '/images/cover.jpg'
      },
      {
        id: 4,
        title: '小星星',
        singer: '小红',
        src: 'http://127.0.0.1:3000/4.mp3',
        coverImgUrl: '/images/cover.jpg'
      },
    ],
    state: 'running',
    playIndex: 0,
    play: {
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '',
      singer: '',
      coverImgUrl: '/images/cover.jpg',
    }
  },
  audioBam: null,
  onReady: function () {
    //播放管理器API
    this.audioBam = wx.getBackgroundAudioManager()
    //默认选择第1曲
    this.setMusic(0)
    //播放失败检测
    this.audioBam.onError(() => {
      console.log('播放失败：' + this.audioBam.src)
    })
    //播放完成自动换下一曲，监听音频自然播放结束的事件
    this.audioBam.onEnded(() => {
      this.next()
    })
    //监听音频播放进度更新事件，获取音乐状态信息
    var updateTime = 0
    this.audioBam.onTimeUpdate(() => {
      var currentTime = parseInt(this.audioBam.currentTime)
      if (!this.sliderChangeLock && currentTime !== updateTime) {
        updateTime = currentTime
        this.setData({
          'play.duration': formatTime(this.audioBam.duration || 0),
          'play.currentTime': formatTime(currentTime),
          'play.percent': currentTime / this.audioBam.duration * 100
        })
      }
    })
  },
  setMusic: function (index) {
    var music = this.data.playlist[index]
    this.audioBam.src = music.src
    this.audioBam.title = music.title
    this.setData({
      playIndex: index,
      'play.title': music.title,
      'play.singer': music.singer,
      'play.coverImgUrl': music.coverImgUrl,
      'play.currentTime': '00:00',
      'play.duratoin': '00:00',
      'play.percent': 0,
      state: 'running'
    })
  },
  play: function () {
    this.audioBam.play()
    this.setData({
      state: 'running'
    })
  },
  pause: function () {
    // console.log(this.audioBam)
    this.audioBam.pause()
    this.setData({
      state: 'paused'
    })
  },
  mode: 0, //播放模式，0顺序播放(默认)，1单曲循环，2随机播放
  modeImgs: ["/images/loop.png", "/images/single.png", "/images/random.png"],
  changeMode: function() {
    this.mode = (this.mode + 1) % 3; //每次下一个环即可
    this.data.modeImg = this.modeImgs[this.mode];
    this.setData({ //刷新
        modeImg: this.modeImgs[this.mode]
    });
  },
  next: function (e) {
    var index = 0;
    switch (this.mode) {
        case 0: //顺序播放：环计算分组, (自定义熟悉中包含回跳与前进步数，正数为下，负数为上)，自动播放无触发对象，需特殊处理
        case 1: //单曲循环：设置为当前播放即可, 跳点值为 0，特殊条件为自动播放事件&手动点击是一个单位
            index = (this.data.playIndex + (e ? Number(e.target.dataset.value) : this.mode === 0 ? 1 : 0) + this.data.playlist.length) % this.data.playlist.length;
            break;
        case 2: //随机播放：不管自动播放还是手动，直接随机
            index = Math.floor(Math.random() * this.data.playlist.length);
            break;
    }
    this.setMusic(index);
  },
  sliderChangeLock: false,
  sliderChanging: function (e) {
    var second = e.detail.value * this.audioBam.duration / 100
    this.sliderChangeLock = true
    this.setData({
      'play.currentTime': formatTime(second),
    })
  },
  sliderChange: function (e) {
    var second = e.detail.value * this.audioBam.duration / 100
    this.audioBam.seek(second)
    setTimeout(() => {
      this.sliderChangeLock = false
    }, 1000)
  },
  change: function (e) {
    this.setMusic(e.currentTarget.dataset.index)
  },
  changeItem: function (e) {
    this.setData({
      item: e.target.dataset.item
    })
  },
  changeTab: function (e) {
    this.setData({
      item: e.detail.current
    })
  }
})
